package com.zhht.htzx.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.page.WebViewApiActivity;
import com.nsgf.library.utils.BaseAlterDialog;
import com.nsgf.library.utils.SPUtil;
import com.nsgf.library.utils.SpKey;
import com.nsgf.library.utils.ToastUtils;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityBusinessLoginBinding;


public class LoginBusinessActivity extends BaseActivity {
    private LoginStates mStates;

    private ActivityBusinessLoginBinding mBinding;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(LoginStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_business_login, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = (ActivityBusinessLoginBinding) getBinding();

        mStates.titleBar.set("登录");
        getLifecycle().addObserver(mAccountRequester);

        mAccountRequester.getUserDataResult().observe(this, dataResult -> {

            if (0 == dataResult.getResponseCode()) {

                UserInfo.getInstance().setTokenValue(dataResult.getData().getAccessToken());
                UserInfo.getInstance().setId(String.valueOf(dataResult.getData().getUserId()));
                UserInfo.getInstance().setIsMerchant(1);

                if (mStates.rememberPassword.get()) {
                    SPUtil.put(SpKey.KEY_USER_LOGIN, "rememberPassword", mStates.rememberPassword.get(), LoginBusinessActivity.this);
                    SPUtil.put(SpKey.KEY_USER_LOGIN, "userName", mStates.name.get(), LoginBusinessActivity.this);
                    SPUtil.put(SpKey.KEY_USER_LOGIN, "userPassword", mStates.password.get(), LoginBusinessActivity.this);
                    SPUtil.put(SpKey.KEY_USER_LOGIN, "tenantName", mStates.tenantName.get(), LoginBusinessActivity.this);
                } else
                    SPUtil.clear(SpKey.KEY_USER_LOGIN, LoginBusinessActivity.this);
                finish();

            } else {
                getController().dismissLoading();
                new BaseAlterDialog(this, R.style.dialogstyle,
                        dataResult.getMsg(), "确定", "登录失败",
                        (dialog, confirm) -> {
                            dialog.dismiss();

                        }).show();
            }
        });
        mStates.name.set(SPUtil.get(SpKey.KEY_USER_LOGIN, "userName", "", LoginBusinessActivity.this));
        mStates.tenantName.set(SPUtil.get(SpKey.KEY_USER_LOGIN, "tenantName", "", LoginBusinessActivity.this));
        mStates.password.set(SPUtil.get(SpKey.KEY_USER_LOGIN, "userPassword", "", LoginBusinessActivity.this));
        mStates.rememberPassword.set(SPUtil.get(SpKey.KEY_USER_LOGIN, "rememberPassword", false, LoginBusinessActivity.this));
    }

    public static class LoginStates extends StateHolder {

        public final State<String> name = new State<>("");//

        public final State<String> password = new State<>("");//

        public final State<String> tenantName = new State<>("");//

        public final State<Boolean> checkAgreement = new State<>(false);
        public final State<Boolean> rememberPassword = new State<>(false);

    }

    public class ClickProxy {

        public void openWebView() {

            startActivity(new Intent()
                    .putExtra("id", String.valueOf(2))
                    .putExtra("title", "用户协议")
                    .setClass(LoginBusinessActivity.this, WebViewApiActivity.class));
        }

        public void login() {


            if (!mStates.checkAgreement.get()) {
                ToastUtils.showShortToast("请勾选用户协议与隐私政策");
                return;
            }

            if (TextUtils.isEmpty(mStates.name.get()) || TextUtils.isEmpty(mStates.password.get())) {

                new BaseAlterDialog(LoginBusinessActivity.this, R.style.dialogstyle,
                        "账户信息输入不完整", "确定", "登录失败",
                        (dialog, confirm) -> {
                            dialog.dismiss();

                        }).show();
                return;
            }

            getController().displayLoading();

            mAccountRequester.businessLogin(mStates.tenantName.get(), mStates.name.get(), mStates.password.get());


        }
    }

}
